<?php

/**
 * This is the model class for table "log".
 *
 * The followings are the available columns in table 'log':
 * @property string $id
 * @property string $action
 * @property string $user_id
 * @property string $time
 * @property string $folder_id
 * @property string $output
 * @property string $path
 *
 * The followings are the available model relations:
 * @property OpenIdUser $user
 * @property Folder $folder
 */
class Log extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Log the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'log';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('action, user_id, time, folder_id, path', 'required'),
			array('action', 'length', 'max'=>255),
//			array('user_id, folder_id', 'length', 'max'=>11),
			array('output', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, action, user_id, time, folder_id, output, path', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'OpenIdUser', 'user_id'),
			'folder' => array(self::BELONGS_TO, 'Folder', 'folder_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'action' => 'Action',
			'user_id' => 'User',
			'time' => 'Time',
			'folder_id' => 'Folder',
			'output' => 'Output',
			'path' => 'Path',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
		$criteria->with = array('folder','user');

//		$criteria->join='INNER JOIN folder ON t.folder_id = folder.id INNER JOIN open_id_user oiu ON t.user_id = oiu.id ';
		if(!isset($_GET['Log_sort'])) $criteria->order = 'time desc';
		$criteria->compare('id',$this->id,true);
		$criteria->compare('action',$this->action,true);
		$criteria->compare('user.email',$this->user_id,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('output',$this->output,true);
		$criteria->compare('folder.name',$this->folder_id,true);
		$criteria->compare('t.path',$this->path,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
			'pageSize'=>15,
			),
		));
	}
}
